package com.chima.action;

import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.chima.domain.Advice;
import com.chima.domain.PageRequestBean;
import com.chima.domain.PageResponseBean;
import com.chima.service.AdviceService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import flexjson.JSONSerializer;
public class AdviceAction extends ActionSupport implements ModelDriven<Advice>{
	Advice advice= new Advice();
	public Advice getModel() {
		return  advice;
	}
	private int start;// 开始页码
	private int limit; //每页显示
	private int page;//页数
	private boolean success=false ;
	private AdviceService adviceService;
	private List<Advice> advices;

	public AdviceService getAdviceService() {
		return adviceService;
	}

	public void setAdviceService(AdviceService adviceService) {
		this.adviceService = adviceService;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public String  ListAdvice() throws Exception{
	    PageRequestBean pageRequestBean = new PageRequestBean();
		pageRequestBean.setStart(start);
		pageRequestBean.setLimit(limit);
		pageRequestBean.setPage(page);
		PageResponseBean pageResponseBean =adviceService.findAdvice(pageRequestBean);
		advices=pageResponseBean.getRows();
        int total=0;
        total =pageResponseBean.getTotal();
		// 输出json内容到客户端
        JSONSerializer jsonSerializer = new JSONSerializer();
        jsonSerializer.include("*.id","*.adcontent");
        jsonSerializer.exclude("*.class");
        String resultJson1 = jsonSerializer.serialize(advices);  
        String resultJson2 =jsonSerializer.serialize(total);
        System.out.println(resultJson2);
        String jsonString = "{total:"+resultJson2.toString()+",advices:"+resultJson1.toString()+"}";
        System.out.print(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
		return NONE;
	
  }
	public void addAdvice() throws Exception{
		try {
			adviceService.saveAdvice(advice);
		} catch (Exception e) {
			
			e.printStackTrace();
		}
		this.success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		System.out.println(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
	}
		public void editAdvice() throws Exception{
		adviceService.updateAdvice(advice);
		this.success=true;
		JSONSerializer jsonSerializer = new JSONSerializer();
		String resultJson =jsonSerializer.serialize(success);
		String jsonString = "{success:"+resultJson.toString()+"}";
		System.out.println(jsonString);
		ServletActionContext.getResponse().setContentType("text/json;charset=utf-8");
		ServletActionContext.getResponse().getWriter().print(jsonString);
	}
	public String deleteAdvice(){
			adviceService.deleteAdvice(advice.getId());
		return NONE;
	}
}



